*GENERAL INFO
	* Project: How to Close the Gender Gap in Political Participation: Lessons from Matrilineal Societies in Africa
	* Authors: Amanda Robinson and Jessica Gottlieb
	* Date created: Dec. 2018
* DO FILE INFO
	* This .do file cleans, combines, and formats MLSFH data for analyses.
    * This file is called by RG_Matlin_Mal_analyses.do.
    * Access the following MLFSH data files at https://malawi.pop.upenn.edu/malawi-data-mlsfh:
        * 1998.MLSFH.men.dta
        * 1998.MLSFH.women.dta
        * 2004.MLSFH.household.listing.dta
        * 2004.MLSFH.main.survey.ever.married.dta
        * MLSFH-datakey.dta

* SET WORKING DIRECTORY
	* Set the working directory to the folder in which all replication files are stored.
    * Put the MLFSH datasets listed above into a folder within the working directory called "MLFSH_Data".
		
****************************************************************
set more off

* Start with Full Set of Respondents across rounds
use "MLFSH_Data/MLSFH-datakey.dta", clear
*merge in data from rounds 1998 and 2004
sort respondentid  
*1998 (M1)
	merge 1:1 respondentid using "MLFSH_Data/1998.MLSFH.women.dta" , nogen 
	merge 1:1 respondentid using "MLFSH_Data/1998.MLSFH.men.dta", nogen nolabel update
	gen m1=(DK_M1_survey_outcome==1) if DK_M1_survey_outcome~=.
*2004 (M3)
	merge 1:1 respondentid using "MLFSH_Data/2004.MLSFH.main.survey.ever.married.dta" , nogen
	gen m3=(DK_M3_survey_outcome==1) if DK_M3_survey_outcome~=.




* Region/District
gen region=DK_region
lab val region Region
lab var region "Region"
gen district=region
lab def district 1 "Mchinji" 2 "Balaka" 3 "Rumphi"
lab val district district

* Ethnicity 
gen egroup04=1 if M3_b4a==1
replace egroup04=2 if M3_b4b==1
replace egroup04=3 if M3_b4c==1
replace egroup04=4 if M3_b4d==1
replace egroup04=5 if M3_b4e==1
replace egroup04=DK_ethnic if egroup04==. & m3==1
recode egroup04 (6 7 8 9=.)
recode egroup04 (5=6) if region==3
recode egroup04 (5=7) if region==2 | region==1
lab def egroup04 1 "Yao" 2 "Chewa" 3 "Lomwe" 4 "Tumbuka" 6 "N. Ngoni" 7 "C. Ngoni"
lab val egroup04 egroup04
lab var egroup04 "Ethnicity, 2004"

* Matrilineal (from Murdock/Ibik)
recode egroup04 (1 2 3 7=1)(4 6=0), gen(matlin)
lab var matlin "Matrilineal (Ethnic Group)"

* Matrilocal (from Murdock/Ibik)
recode egroup04 (1 2 3 7=1)(4 6=0), gen(matloc)
lab var matloc "Matrilocal Residence (Ethnic Group)"

generate matgroup=0 
replace matgroup=1 if matlin==1 & matloc==1
lab var matgroup "Matrilineal/local Group"

* Gender
recode DK_gender (0=1)(1=0), gen(female)
lab def female 0 "Male" 1 "Female"
lab val female female
lab var female "Gender, Female"

* Land Ownership/Amount/Source
recode M3_e5 (99=1), gen(ownland) 
lab val ownland E5
lab var ownland "Owns Land"
gen landamount=M3_e6a
replace landamount=0 if ownland==0
lab var landamount "Amount of Land Owned, in Football Pitches"
recode M3_e7 (4=3)(5=4) (99=.), gen(landsource) 
recode landsource (2=1)(1=2) if female==0
lab def landsource 0 "No Land" 1 "Inherited, Woman's Kin" 2 "Inherited, Man's Kin" 3 "Purchased" 4 "Given by Chief" 
lab val landsource landsource
recode landsource (1=1)(2/4=0), gen(kinland)
recode kinland (0=1) if female==0 & landsource==2
recode kinland (1=0) if female==0 & landsource==1
replace kinland=0 if ownland==0
lab val kinland yesno
lab var kinland "Owns Land through Matriline"
gen nonkinland=(ownland==1&kinland==0) if female==1
lab val nonkinland yesno
lab var nonkinland "Owns Land NOT through Matriline"
gen land=landsource
recode land (.=0) if ownland==0
lab val land landsource

* Marital Residence
recode M3_m19 (1 3  =0) (2 4 =1) (5=2) ( 99 =.), gen(residence)
lab def residence 0 "Patrilocal" 1 "Matrilocal" 2 "Neolocal"
lab val residence residence
lab var residence "Residence Pattern"
recode M3_m19 (1 3 5 =0) (2 4 =1) ( 99 =.), gen(matrilocal)
lab var matrilocal "Matrilocal Residence"
lab val matrilocal yesno
recode M3_m19 (1 3  =1) (2 4 5 =0) ( 99 =.), gen(patrilocal)
lab var patrilocal "Patrilocal Residence"

*Attended meeting at Headman's bwalo
recode M3_g5c1 (0 6=0)(1/5=1)(99=.), gen(chiefmeeting)
lab var chiefmeeting "Attended Chief Meeting"

* Justification for Leaving Spouse
recode M3_x2a M3_x2b M3_x2c M3_x2d M3_x2e M3_x2f M3_x2g M3_x2h (88 99=.)
lab var M3_x2a "Justified to Leave, No Financial Support"
lab var M3_x2b "Justified to Leave, Beating"
lab var M3_x2c "Justified to Leave, Unfaithful"
lab var M3_x2d "Justified to Leave, AIDS"
lab var M3_x2e "Justified to Leave, Family Planning"
lab var M3_x2f "Justified to Leave, STD"
lab var M3_x2g "Justified to Leave, No Children"
lab var M3_x2h "Justified to Leave, No Sex"
pca M3_x2a-M3_x2h
predict justleave
lab var justleave "Justified in Leaving Husband (Index)"


* Divorce
generate divorce=0
foreach var of varlist M3_m14aa-M3_m14ae{
replace divorce=1 if `var'==1
}
lab var divorce Divorced

egen divorcegroup=mean(divorce) if female==1, by(egroup04)
lab var divorcegroup "Divorce Rate of Women in Ethnic Group"
egen divorcevillage=mean(divorce) if female==1, by(DK_village_number)
lab var divorcevillage "Divorce Rate of Women in Village"
	*fill in values for men
bysort egroup04 (divorcegroup): replace divorcegroup = divorcegroup[1] if female==0
bysort DK_village_number (divorcevillage): replace divorcevillage = divorcevillage[1] if female==0


* Group and Village-Level Measures of % Matrilineal Land Inheritance
egen pmatvillage=mean(kinland) if female==1, by(DK_village_number)
lab var pmatvillage "Prop. of Women in Village with Matrilineally Inherited Land"
egen pmatgroupvillage=mean(matgroup) if female==1, by(DK_village_number)
lab var pmatgroupvillage "Prop. of Women in Village from a Matrilineal Ethnic Group"
	*fill in values for men
bysort DK_village_number (pmatvillage): replace pmatvillage = pmatvillage[1] if female==0
bysort DK_village_number (pmatgroupvillage): replace pmatgroupvillage = pmatgroupvillage[1] if female==0

* Village-level measure of divorce rate (among sample)
egen pdivvillage=mean(divorce) if female==1, by(DK_village_number)
bysort DK_village_number (pdivvillage): replace pdivvillage = pdivvillage[1] if female==0
lab var pdivvillage "Prop. of Divorced Women in Village"

* matrilocality
egen plocvillage=mean(matrilocal) if female==1, by(DK_village_number)
lab var plocvillage "Prop. of Women in Village with Matrilocal Residence"
	*fill in values for men
bysort DK_village_number (plocvillage): replace plocvillage = plocvillage[1] if female==0

tempfile main
save `main'

* Code Men at Ind. Level by Wife's Land Inheritance 
	* get women's land inheritance by id
	keep if female==1 & m3==1
	keep respondentid kinland 
	gen DK_M3_spouseid_1=respondentid
	gen DK_M3_spouseid_2=respondentid
	gen DK_M3_spouseid_3=respondentid
	gen DK_M3_spouseid_4=respondentid
	drop respondentid
    tempfile w
    save `w'
use `main'
	* get men
	keep if female==0 & m3==1
	keep respondentid DK_M3_spouseid_1 DK_M3_spouseid_2 DK_M3_spouseid_3 DK_M3_spouseid_4
    tempfile m
    save `m'

	* merge in spouse's land
	merge m:1 DK_M3_spouseid_1 using `w', keepusing(kinland) keep(master match) nogen
	rename kinland kinland1
	merge m:1 DK_M3_spouseid_2 using `w', keepusing(kinland) keep(master match) nogen
	rename kinland kinland2
	merge m:1 DK_M3_spouseid_3 using `w', keepusing(kinland) keep(master match) nogen
	rename kinland kinland3
	merge m:1 DK_M3_spouseid_4 using `w', keepusing(kinland) keep(master match) nogen
	rename kinland kinland4
    gen wifekinland=(kinland1==1 | kinland2==1 | kinland3==1 | kinland4==1)
    gen wife_insample=(DK_M3_spouseid_1~="")
    keep respondentid wifekinland wife_insample
	tempfile m2
	save `m2'
use `main'
merge m:1 respondentid using `m2' 

replace kinland=wifekinland if female==0
lab var kinland "Inherited Matrilineal Land"

gen female2=female // to allow for instrumented interaction with gender

gen genland=0 if female==0 
recode genland (.=1) if kinland==0
recode genland (.=2) if kinland==1

*Variables for triple interaction (female only measures)
clonevar kinlandfem=kinland
replace kinlandfem=0 if female==0
label define kinlandfem 1 "Matrilineal Land"
label values kinlandfem kinlandfem
clonevar localfem=matrilocal
replace localfem=0 if female==0
label define localfem 1 "Matrilocal Residence"
label values localfem localfem

*Generate variables on education investments within households
preserve
use "MLFSH_Data/2004.MLSFH.household.listing.dta", clear
generate eddaughter=.
replace eddaughter= M3_yearatlevel_ if M3_relationshiphead_==3 & M3_sex_==2 &  M3_leveleduc_==1 & M3_yearatlevel<10
replace eddaughter= M3_yearatlevel_ + 8 if M3_relationshiphead_==3 & M3_sex_==2 &  M3_leveleduc_==2 & M3_yearatlevel<10

generate edson=.
replace edson= M3_yearatlevel_ if M3_relationshiphead_==3 & M3_sex_==1 &  M3_leveleduc_==1 & M3_yearatlevel<10
replace edson= M3_yearatlevel_ + 8 if M3_relationshiphead_==3 & M3_sex_==1 &  M3_leveleduc_==2 & M3_yearatlevel<10

replace eddaughter=eddaughter/12
replace edson=edson/12

collapse (mean) eddaughter edson, by(respondentid)

sort respondentid
tempfile temp1
save `temp1'
restore

merge 1:1 respondentid using `temp1', nogen keepusing(eddaughter edson)

generate edgap=eddaughter-edson
lab var edgap "Avg. Difference in Daughter-Son Yrs. of Education"

keep if m3==1

* export a codebook
/*
set more off
log using codebook, replace
codebook, n
log close
translate codebook.smcl "RG_Matlin_Mal_codebook.pdf", replace
*/
